var util = require('../../utils/util');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import { $wuxLoading } from '../../dist/index'

var app = getApp();
var that;
var pageInex = 1;

Page({
  data: {
    money: 0,
	empty_msg:app.globalData.empty_msg,
	user:null,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false,
    inputShowed: false,
    inputVal: "",
    codename:"尝试启动",
    wetype:"primary"
  },

  onPullDownRefresh:function(){
    pageInex = 1;
    this.start=true;
    that.setData({
      list : [],
      isLoadMore : false,
      isEnd : false,
      isEmpty:true
    });
    util.showLoading();
    that.loadData();
  },
  onReachBottom : function(e,a){
      if(!that.data.isEnd && !that.data.isLoadMore && !that.data.isEmpty){
          that.loadData();
      }
  },

  onLoad:function(){
	  util.loading = $wuxLoading();
    pageInex = 1;
    that = this;
    
	that.loadData();
	
  },
  loadData:function(){
    util.showLoading();
    app.getUserInfo()
    .then(res => {
      if(app.globalData.num >0){
        that.setData({
          disabled: true,
          wetype:"default"
        });
      }

		var params = {
		  pageIndex: pageInex,
		  pageSize: app.globalData.defaultPageSize
		}
		return wxRequest.postRequest(config.repeatOrderList, params)
	})
    .then(res => {
        res = res.list;

        if(pageInex==1 && res.length==0){
            that.setData({
              isLoadMore : false,
              isEnd : false,
              isEmpty :true
            });
            return;
        }

        if(res.length > 0){
          pageInex++;

          for(var i in res){
            res[i].create_at = res[i].create_at.substring(0,16);
          }

          var data = that.data.list.concat(res);   
          that.setData({
              list : data,
              isLoadMore : false
			  ,isEmpty :false
          });
        }
        
        if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
          that.setData({
            isLoadMore : false,
            isEnd : true,
			      isEmpty :false
          });
        }

    })
    .catch(res => {
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });

  }
  ,repeart:function(e){
    
    that.setData({
      disabled: true,
      wetype:"default"
    });
  
    var params = {};
    params.order_id = e.currentTarget.id;
 
    wxRequest.postRequest(config.repeat, params)
    .then(res => {

      if(res.data.re==1){
        app.globalData.num = 60;
      }

      if(res.data.re==2 ){
        app.globalData.num = 10;
      }

      if(res.data.re==3 ){
        app.globalData.num = 30;
      }

      var timer = setInterval(function () {
        app.globalData.num--;
        if (app.globalData.num <= 0) {
          app.globalData.num=0;
          clearInterval(timer);
          that.setData({
            codename: '尝试启动',
            disabled: false,
            wetype:"primary"
          })
        } else {
          that.setData({
            codename: app.globalData.num + "s"
          })
        }
      }, 1000);

      util.hideLoading();
      util.alert(res.data.msg,function(){
      });

    })
    .catch(res => {
        util.alert_catch(res.message);
        that.setData({
          codename: '尝试启动',
          disabled: false,
          wetype:"primary"
        });
    })
    .finally(function (res) {
        util.hideLoading();
    }); 

  }
 


})








 // ,getVerificationCode: function () {
  //   var num = 60;
  //   that.setData({
  //     codename: num + "s",
  //     disabled: true
  //   })
  //   var timer = setInterval(function () {
  //     num--;
  //     if (num <= 0) {
  //       clearInterval(timer);
  //       that.setData({
  //         codename: '再启动',
  //         disabled: false
  //       })
  //     } else {
  //       that.setData({
  //         codename: num + "s",
  //         wetype:"primary",
  //         disabled: true
  //       })
  //     }
  //   }, 1000);
  // }